<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Models\Order;
use App\Models\OrdersGood;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use App\Models\Category;

class OrderController extends Controller
{
    protected $name = '订单管理';
    use ModelForm;


    public function index()
    {
        if(request()->get('ismobile', 0) == 1)
        {
          $pageList = Order::where('type', request()->get('type', 0))->where('status', 0)->orderBy('id', 'desc')->paginate(15);
          $pageList1 = Order::where('type', request()->get('type', 0))->where('status', 1)->orderBy('id', 'desc')->paginate(15);
          $pageList2 = Order::where('type', request()->get('type', 0))->where('status', 2)->orderBy('id', 'desc')->paginate(15);
          $pageList3 = Order::where('type', request()->get('type', 0))->where('status', -1)->orderBy('id', 'desc')->paginate(15);
          return view('admin.home.order', compact('pageList', 'pageList1', 'pageList2', 'pageList3'));
        }
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description($this->name);


            $content->body($this->grid());
        });
    }

    public function detail()
    {
      $info = Order::findOrFail(request()->get('id', 0));

      if(request()->get('type', '') == 'accpet')
      {
        $y = (int)request()->get('freight', 0);
        if($y < 0)return ['msg'=> '运费大于等于0'];
        $info->ems_price = $y;
        $info->status = 1;
        $info->save();
        return ['msg'=> '接单成功'];
      }elseif(request()->get('type', '') == 'orderUpdate')
      {
        $attr = OrdersGood::where('orders_id', $info->id)->where('id', request()->get('ODid', 0))->delete();
        $info->pay_price = sumTotal(OrdersGood::where('orders_id', $info->id)->get());
        $info->save();
        return ['msg'=> '删除成功', 'status'=>1];
      }elseif(request()->get('type', '') == 'cancel')
      {
        $info->status = -1;
        $info->save();
        return ['msg'=> '取消成功', 'status'=>1];
      }elseif(request()->get('type', '') == 'send')
      {
        $info->ship_nmber = request()->get('logistics', '');
        $info->status = 3;
        $info->save();
        return ['msg'=> '取消成功', 'status'=>1];
      }

      return view('admin.home.detail', compact('info'));
    }

    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->name);
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description('创建');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Order::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'DESC');

            $grid->filter(function($filter){
                $filter->disableIdFilter();
                $filter->like('name', '标题');
                //$filter->addFilter($filter->like('post_title', '标题'));
                $filter->equal('type', '类型')->select(Order::$type);

            });
            $grid->column('id', 'ID')->sortable();
            $grid->column('user.nickname', '昵称')->display(function($value){
                if(empty($value))
                {
                    return empty($this->user['name'])?$this->user['mobile']:$this->user['name'];
                }else{
                    return $value;
                }
            });
            $grid->column('status', '订单状态')->options()->select(Order::$status);

            $grid->column('pay_price', '金额');
            $grid->column('ems_price', '运费');

            $grid->column('updated_at', '更新时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        /* foreach (Order::first()->goods as $row){
            dd($row->good->title);
        } */
        return Admin::form(Order::class, function (Form $form) {

            $form->number('id', 'ID')->attribute('disabled', false);
            $form->currency('pay_price', '总金额');
            $form->currency('ems_price', '运费');
            $form->text('ship_nmber', '物流单号');
            $form->radio('status', '订单状态')->options(Order::$status);
            $form->radio('type', '订单状态')->options(Order::$type);
            $form->display('address.name', '收货人姓名');
            $form->display('address.mobile', '收货人电话');
            $form->display('address.address', '收货人地址');
            $form->display('address.code', '收货人邮编');
            $form->hasMany('orders_goods', '商品列表', function (Form\NestedForm $form) {
              $form->number('goods_num', '数量');
                $form->display('goods_id', '商品名称')->with(function($value){
                    foreach ($this->orders_goods as $row)
                    {
                        if($row->goods_id == $value)
                        {
                            $order_good = $row;
                            return view('admin.home.good', compact('order_good'))->render();
                        }
                    }
                });
            });

        });
    }
}
